/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author Ripper
 */
public class GameLobbyPanel extends ClientPanel implements ActionListener
{
    private JLabel sessionNameLabel;
    
    private GameLobbyPanelUpdater updater;
    
    private JLabel dungeonMasterLabel;
    private JList activeplayersList;
    
    private JButton startGameButton;
    private JButton disconnectButton;
    private JButton readupButton;
    private boolean ready = false;
    
    private JButton becomeorresignasDMbutton;
    private boolean isDM = false;
    
    public GameLobbyPanel(ClientWindow parent)
    {
        super(parent);
        setLayout(new BorderLayout());
        
        JPanel topPanel = new JPanel(new BorderLayout());
        {
            topPanel.add(new JLabel(" Dungeon Master: "), BorderLayout.WEST);
            
            dungeonMasterLabel = new JLabel();
            topPanel.add(dungeonMasterLabel, BorderLayout.CENTER);
            
            becomeorresignasDMbutton = new JButton(" apply for DM ");
            becomeorresignasDMbutton.addActionListener(this);
            topPanel.add(becomeorresignasDMbutton, BorderLayout.EAST);
            
            sessionNameLabel = new JLabel("");
            topPanel.add(sessionNameLabel, BorderLayout.NORTH);
        }
        add(topPanel, BorderLayout.NORTH);
        
        activeplayersList = new JList();
        JScrollPane scroll = new JScrollPane(activeplayersList);
        
        add(scroll, BorderLayout.CENTER);
        
        JPanel botPanel = new JPanel(new BorderLayout());
        {
            JPanel containment = new JPanel(new GridLayout(1,3));
            {
                startGameButton = new JButton(" start game! ");
                startGameButton.addActionListener(this);
                containment.add(startGameButton);
                
                readupButton = new JButton(" readyup! ");
                readupButton.addActionListener(this);
                containment.add(readupButton);
                
                disconnectButton = new JButton(" disconnect ");
                disconnectButton.addActionListener(this);
                containment.add(disconnectButton);
            }
            botPanel.add(containment,BorderLayout.CENTER);
        }
        add(botPanel, BorderLayout.SOUTH);
    }
    
    
    public void provideData(String data)
    {
        if(data.startsWith("gamestarting"))
        {
            stopViewingData();
            getClientWindow().setDungeonMaster(isDM);
            getClientWindow().getClientDomainController().startTheGame();
            getClientWindow().gotoGameScreen();
        }
        else
        {
            String[] bits = data.split("-");

            dungeonMasterLabel.setText("<No dungeon master>");

            List<String> playerList = new ArrayList<String>();
            for(String s: bits)
            {
                boolean isthisDM = s.startsWith("dm");
                String stringdata = s.split(":")[1];

                String playerName = stringdata.split(",")[0];
                boolean readied = stringdata.split(",")[1].startsWith("ready");
                
                boolean isThisYou = getClientWindow().getClientDomainController().getUsername().equals(playerName);

                if(isthisDM)
                {
                    dungeonMasterLabel.setText(" "+playerName+" ("+(readied?"ready!":"NOT READY")+") "+
                            (isThisYou?"(you!)":"")+"");
                }
                else
                {
                    playerList.add(" "+playerName+" ("+(readied?"ready!":"NOT READY")+") " + (isThisYou?"(you!)":"")+"");
                }
            }

            int selectedIndex = activeplayersList.getSelectedIndex();
            activeplayersList.setListData(playerList.toArray());
            activeplayersList.setSelectedIndex(selectedIndex);
        }
    }
    
    public void startViewingData(String sessionName)
    {
        sessionNameLabel.setText(sessionName);
        if(updater == null || !updater.isRunning())
        {
            updater = new GameLobbyPanelUpdater(this, getClientWindow().getClientDomainController());
            Thread t = new Thread(updater);
            
            t.start();
            
            ready = false;
            readupButton.setText(" readyup! ");
            isDM = false;
            becomeorresignasDMbutton.setText(" apply for DM ");
        }
    }
    public void stopViewingData()
    {
        if(updater != null)
        {
            updater.stopRunning();
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == startGameButton)
        {            
            try
            {
                getClientWindow().startTheGame();
            }
            catch(Exception ex){
                getClientWindow().displayErrorMessage("Game is not ready to be started!");
            }
        }
        else if(e.getSource() == disconnectButton)
        {
            stopViewingData();
            getClientWindow().disconnectFromSession();
        }
        else if(e.getSource() == readupButton)
        {
            try
            {
                if(!ready)
                {
                    if(getClientWindow().readyup())
                    {
                        ready = !ready;
                        readupButton.setText(" cancel ready up.. ");
                    }
                }
                else
                {
                    if(getClientWindow().cancelreadyup())
                    {
                        ready = !ready;
                        readupButton.setText(" readyup! ");
                    }
                }
            }
            catch(Exception ex)
            {
                
            }
        }
        else if(e.getSource() == becomeorresignasDMbutton)
        {
            if(getClientWindow().becomeorresignasDM(isDM))
            {
                if(isDM)
                {
                    isDM = false;
                    becomeorresignasDMbutton.setText(" apply for DM ");
                }
                else
                {
                    isDM = true;
                    becomeorresignasDMbutton.setText(" resign as DM ");
                }
            }
            else
            {
                if(!isDM)
                {
                    getClientWindow().displayErrorMessage("Someone is already DM!");
                }
            }
        }
    }
}

//TODO: when leaving this panel, close the update thread!